﻿/// <reference path="P3.Core.Init.js" />
/// <reference path="P3.Core.js" />
/// <reference path="P3.Core.Settings.js" />
/// <reference path="jquery.fabric.min.js" />
/// <reference path="JQuery/jquery-ui.min.js" />
/// <reference path="P3.Core.SharePointClient.js" />
/// <reference path="JQuery/jquery.min.js" />


//this method sets the cookie
function setCookie(cname, cvalue, exdays, path) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires + "; " + path;
}

//this method gets the cookie value
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1);
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

//this method checks if a cookie exists
function checkCookie(cname) {
    var cooki = getCookie(cname);
    if (cooki != "") {
        return true
    } else {
        return false;
    }
}

function getQueryStringValueByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

function updateQueryStringParameter(uri, key, value) {
    var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
    var separator = uri.indexOf('?') !== -1 ? "&" : "?";
    if (uri.match(re)) {
        return uri.replace(re, '$1' + key + "=" + value + '$2');
    }
    else {
        return uri + separator + key + "=" + value;
    }
}

// Read a page's GET URL variables and return them as an associative array.
function getAllQueryStrings() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

/*************************************Multi lookup Picker -- KSI ****************************************/

var selector = "[id$='MultiLookupPicker']";

function addChoice(text) {
    $("[title='Referred Parts possible values'] option").each(function () {
        if ($(this).text() == text) {
            $(this).appendTo($("[title='Referred Parts selected values']"));
            var multilookupPickerVal = $(selector).val();
            if ($(selector).val() == undefined || $(selector).val().length == 0) {
                $(selector).val($(this).val() + "|t" + $(this).text());
            }
            else {
                $(selector).val(multilookupPickerVal + "|t" + $(this).val() + "|t" + $(this).text());
            }
        }
    });
}

function removeChoice(text) {
    $("[title='Referred Parts selected values'] option").each(function () {
        if ($(this).text() == text) {
            $(this).appendTo($("[title='Referred Parts possible values']"));
            var multilookupPickerVal = $(selector).val();
            var valToRemove = $(this).val() + "|t" + $(this).text();
            var newValue = multilookupPickerVal.replace(valToRemove, "");

            $(selector).val(newValue);
        }
    });
}



/*********************************MISC*******************************************************************/

//sorting the object array
function sortByTitle(a, b) {
    var aTitle = a.Title.toLowerCase();
    var bTitle = b.Title.toLowerCase();
    return ((aTitle < bTitle) ? -1 : ((aTitle > bTitle) ? 1 : 0));
}

function getSelectedDropdownValue(selectTagId) {
    var Selectedvalue = $('#' + selectTagId + ' option:selected').val();
    return Selectedvalue;
}

function getSelectedDropdownAttribute(selectTagId, Attribute) {
    var attributeVal = $('#' + selectTagId + ' option:selected').attr(Attribute);
    return attributeVal;
}


function populateDropDownList(SelectTagId, OptionsArray) {
    for (i = 0; i < OptionsArray.length; i++) {
        try {
            $('#' + SelectTagId).append
    	     ($('<option>', { value: OptionsArray[i].Value, text: OptionsArray[i].Title }));
        } catch (e) {
            continue;
        }
    }
}

function nextMonth() {
    var thisMonth = this.getMonth();
    this.setMonth(thisMonth + 1);
    if (this.getMonth() != thisMonth + 1 && this.getMonth() != 0)
        this.setDate(0);
}

function prependZero(n) {
    if (n < 10)
        return "0" + n;
    return n;
}

function createMoreLessButtons(listTitle, fieldsArr) {
    var lisTable = $("table[summary='" + listTitle + "']");
    var hdrNames = fieldsArr;
    var fieldIndex1 = [];
    var count = 1;

    for (var j = 0; j < hdrNames.length; j++) {
        fieldIndex1.push($(lisTable).find("div[name='" + hdrNames[j] + "']").parent().index() + 1);
    }

    $(lisTable).find('tr').each(
		function () {
		    if ($(this).attr('class') != "ms-viewheadertr ms-vhltr") {
		        if ($(this).attr('id') != "group0") {
		            if (!($(this).parent('tbody').attr("id"))) {
		                for (var k = 0; k < fieldIndex1.length; k++) {
		                    var curField = $(this).children('td:nth-child(' + fieldIndex1[k] + ')');
		                    var fullText = $(curField).children('div').html();

		                    if (fullText != null) {
		                        fullText = fullText.replace(/\n/g, "<BR>");
		                        var text = $(curField).children('div').html().replace(/<BR>/gi, "\n");
		                        text = text.substring(0, 400);//char limit
		                        var lessText = text.replace(/\n/g, "<BR>");
		                        $(curField).children('div').html(lessText);

		                        if (lessText != fullText) {
		                            $(curField).append("<div class='morebtn'>" +
									"<a href='#' onclick='showMoreText(" + count + ")'>More</a></div>")
									.append("<div data-idvalue=" + count + " class='dispvalue'>" + fullText + "</div>")
                                    .append("<div class='lessbtn'><a href='#' onclick='showLessText(" + count + ")'>Less</a></div>");
		                        }
		                    }
		                }
		                count++;
		            }
		        }
		    }
		}
	);

    $(".dispvalue").hide();
    $(".lessbtn").hide();

}

function showMoreText(values) {
    divWithvalue = $("div").find("[data-idvalue='" + values + "']");

    for (var i = 0; i < divWithvalue.length; i++) {
        $(divWithvalue[i]).show();
        $(divWithvalue[i]).next().show();
        $(divWithvalue[i]).prev().hide();
        $(divWithvalue[i]).prev().prev().hide();
    }
}

function showLessText(values) {
    divWithvalue = $("div").find("[data-idvalue='" + values + "']");

    for (var i = 0; i < divWithvalue.length; i++) {
        $(divWithvalue[i]).hide();
        $(divWithvalue[i]).next().hide();
        $(divWithvalue[i]).prev().show();
        $(divWithvalue[i]).prev().prev().show();
    }
}


